<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/20
 * Time: 下午8:46
 */

namespace App\Http\Controllers;


use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\MsgServices;
use App\Lib\Response;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;


class MsgController  extends Controller
{
    private $msg_service;
    public function __construct(MsgServices $msg_service){
        $this->msg_service = $msg_service;
    }

    /**
     * 系统文本消息
     * @param Request $request
     */
    public function sys_text_msg(Request $request){
        $user_id = $request->post('user_id');
        $msg = (string) $request->post('msg');
        $link = $request->post('link', '');
        $link_tip = $request->post('link_tip', '');
        if(!$user_id || !$msg){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $res = $this->msg_service->send_sys_text_msg($user_id,$msg, $link, $link_tip);
        //Log::debug($res);
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 简单的全服消息通知
     * @param Request $request
     */
    public function simpleFullServiceMsgNotify(Request $request){
        $user_id = $request->get('user_id');
        $text = $request->get('text','');

        if(!$user_id){
            OutJson::outJsonResult(Hint::PARAM_ERROR);
        }

        $res = $this->msg_service->simpleFullServiceMsgNotify($user_id,$text);

        //Log::debug($res);

        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 计划任务，保活聊天室
     */
    public function keep_alive_chat_cron(){
        if(!$this->msg_service->query_chat_room()) {
            if(!$this->msg_service->create_chat_room()) exit(1);
            if(!$this->msg_service->join_chat_room()) exit(2);
        }
        $this->msg_service->keep_alive_chat_room();
    }


    /**
     * 发送全员警告
     * @param Request $request
     */
    public function allWarningMsg(Request $request){
        $title = (string) $request->post('title');
        $content = (string) $request->post('content');
        if(!$title || !$content){
            OutJson::outJsonResult(Hint::PARAM_ERROR);
        }
        $res = $this->msg_service->allWarningMsg($title,$content);
        OutJson::outJsonResult(Hint::SUCCESS);
    }


    /**
     * 发送警告
     * @param Request $request
     */
    public function warningMsg(Request $request){
        $userId = (string) $request->post('user_id');
        $title = (string) $request->post('title');
        $content = (string) $request->post('content');
        $link = (string) $request->post('link');
        $link_tip = (string) $request->post('link_tip');
        if(!$title || !$content || !$userId){
            OutJson::outJsonResult(Hint::PARAM_ERROR);
        }
        $res = $this->msg_service->warningMsg($userId, $title, $content, $link, $link_tip);
        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 接口融云聊天消息
     * https://www.rongcloud.cn/docs/server.html#message_sync
     * @param Request $request
     */
    public function receiveMsg(Request $request){
        $code = Hint::SUCCESS;
        $msg = Hint::getErrCodeMsg($code);
        $data = [];

        do {
            write_log('receiveMsg', 'receiveMsg', json_encode($request->post()));
        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 简单的全服消息通知
     * @param Request $request
     */
    public function sendFullScreen(Request $request){
//        $user_id = $request->get('user_id');
        $name = $request->get('name','');
        $num= $request->get('num','');
        $id = $request->get('id' ,5);
        if(!$name || !$num){
            OutJson::outJsonResult(Hint::PARAM_ERROR);
        }

        $res = $this->msg_service->sendFullScreen($id,null,['name'=>$name, 'num' =>$num],[]);

        //Log::debug($res);

        OutJson::outJsonResult(Hint::SUCCESS);
    }

    /**
     * 测试发送飘萍
     *
     */
    public function testPullScreen(Request $request)
    {
        $params = $request ->all();
        $id = $params['id'] ?? '';
        $textParam =$params['textParam'] ?? [];
        $actionParam =$params['actionParam'] ?? [];
        $extendParam = $params['extendParam'] ?? [];
        if (empty($id)){
            var_dump('$id参数不正确' );exit();
        }
        $iconUrl = $params['iconUrl'] ?: '';
        $res = (new MsgServices())->sendFullScreen($id, $iconUrl, $textParam, $actionParam, $extendParam);
        var_dump('执行结果', $res);
    }

    /**
     * 发送萌新私信
     */
    public function novicePrivateMsg(Request $request)
    {
        $userId = $request->get('user_id',0);
        $toUserId = $request->get('to_user_id',0);
        $msg = $request->get('msg','');

        if (empty($userId) || empty($toUserId) || empty($msg)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $res = (new MsgServices())->novicePrivateMsg($userId, $toUserId, $msg);
        Response::returnResult(Hint::SUCCESS,$res);
    }

}
